<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class case_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();

	}
	public function get_country()
	{
	return $this->db->get('country')->result();
	}
	public function get_engineer()
	{
	$this->db->select('users.*');
	$this->db->from('users', 'users_groups' );
	$this->db->join('users_groups', 'users.id= users_groups.user_id   '  );
	$this->db->where('users_groups.group_id',2);
	$q = $this->db->get();
		
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
			return $data;
			
		
	
	
	
	}	


	public function get_countrys()
	{
	$query= $this->db->get('country');
	return $query;
	}
	public function create_service($insert)
	{
	$this->db->insert('customer',$insert);
	return  $this->db->insert_id();
	}
	public function create_case($case)
	{
	
	$this->db->insert('case',$case);
		return  $this->db->insert_id();
	}
	public function nonwarranty_case($case)
	{
	
	$this->db->insert('nonwarranty_case',$case);
		return  $this->db->insert_id();
	}
	public function create_diagnosis($insert)
	{
	$this->db->insert('diagnosis',$insert);
	return  $this->db->insert_id();
	}
	public function nonwarranty_diagnosis($insert)
	{
	$this->db->insert('nonwarranty_diagnosis',$insert);
	return  $this->db->insert_id();
	}
	
	
	public function create_accesories($data)
	{
	$this->db->insert('customer_accesories',$data);
	
	}
	
public function get_caseid()
	{
	$this->db->select_max('id');
    return $this->db->get('case');
	}	
	public function nonwarranty_caseid()
	{
	$this->db->select_max('id');
    return $this->db->get('nonwarranty_case');
	}	
	
	
	
	public function get_id()
	{
	
	$this->db->select_max('service_id');
$query = $this->db->get('customer');
	
	

  return $query;

	}
	public function get_view()
	{
	$this->datatables
			->select("customer.id as id,case.case_no,customer.service_date,customer.customer_name,status.name,users.username,customer.mobile,customer.customer_address")
			->from('customer','case','status','users')	
            ->join('case','customer.service_id=case.service_id')
            ->join('status','case.status_id=status.id')	
            ->join('users'	,'case.engineer_id=users.id')	
			->add_column("Actions",
			"<center>			<a href='index.php/admin/edit_customer/$1'>
								<i class='icon-edit'title='Edit' ></i>
								</a>
								<a href='index.php/admin/print_case/$1'>
								<i class='icon-print 'title='Print'></i>
								</a>
							    <a href='index.php/admin/delete_customer/$1' onClick=\"return confirm('You are going to delete case id:$1')\">
								<i class='icon-trash 'title='Delete'></i>
								</a></center>", "id")
			
			;
	   return  $this->datatables->generate();
	}
	public function get_nonwarrantyview()
	{
	$this->datatables
			->select("customer.id as id,nonwarranty_case.case_no,customer.service_date,customer.customer_name,status_nonwarranty.name,users.username,customer.mobile,customer.customer_address")
			->from('customer','nonwarranty_case','status_nonwarranty','users')	
            ->join('nonwarranty_case','customer.service_id=nonwarranty_case.service_id')
            ->join('status_nonwarranty','nonwarranty_case.status_id=status_nonwarranty.id')	
            ->join('users'	,'nonwarranty_case.engineer_id=users.id')	
			->add_column("Actions",
			"<center>			<a href='index.php/admin/edit_customer/$1'>
								<i class='icon-edit ' title='Edit'></i>
								</a>
								<a href='index.php/admin/print_nonwarranty_case/$1'>
								<i class='icon-print' title='Print'></i>
								</a>
							    <a href='index.php/admin/delete_customer/$1' onClick=\"return confirm('You are going to delete case id:$1')\">
								<i class='icon-trash ' title='Delete'></i>
								</a></center>", "id")
			
			;
	   return  $this->datatables->generate();
	}
	public function delete_case($id)
	{
	$this->db->delete('customer',array('id'=>$id));		
	
	}
	public function edit_case($id)
	{
	$query = $this->db->get_where('customer', array('id' => $id));
		return $query;
	}
	public function edit_access($id)
	{
	$query = $this->db->get_where('customer_accesories', array('customer_id' => $id));
		return $query;
	}
	public function update($edit_id, $insert){
		$this->db->update('customer',$insert,array('id'=>$edit_id));
	}
	
	public function update_acces($edit_id, $insert){
		$this->db->update('customer_accesories',$insert,array('id'=>$edit_id));
	
	
	}
	
    public function get_casedetail($id)
	{
	$query = $this->db->get_where('customer', array('id' => $id));
		return $query->result();
	}
	 public function get_accesoriesdetail($id)
	{
	$query = $this->db->get_where('customer_accesories', array('customer_id' => $id));
		return $query->result();
	}
	
public function get_case_no($id)
{
$this->db->select('case.case_no,case.id,case.service_id,country.name');
	$this->db->from('case', 'customer','country' );
	$this->db->join('customer', 'customer.service_id= case.service_id'  );
	$this->db->join('country', 'customer.country_purchase= country.id'  );
	$this->db->where('case.status_id != 22');
	$this->db->where('customer.id',$id);
	$q = $this->db->get();
  return $q;
}
public function get_nonwarrantycase_no($id)
{
$this->db->select('nonwarranty_case.case_no,nonwarranty_case.service_id,country.name');
	$this->db->from('nonwarranty_case', 'customer','country' );
	$this->db->join('customer', 'customer.service_id= nonwarranty_case.service_id'  );
	$this->db->join('country', 'customer.country_purchase= country.id'  );
	//$this->db->where('case.status_id != 22');
	$this->db->where('customer.id',$id);
	$q = $this->db->get();
  return $q;
}	
public function company_profile()
{
return $this->db->get('company_profile');

}
public function get_so_detail($id)
{
$this->db->select('so.name,so.code,so.so_no,so.case_id');
	$this->db->from('so');
	//$this->db->join('case','case.id=so.case_id');
	//$this->db->where('case.status_id != 22');
	$this->db->where('so.case_id',$id);
	$q = $this->db->get();
	
  return $q->result();

}


	
	}